﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.Common;
using DreamBuilder.Core.DAO;
using DreamBuilder.Core;

namespace Web.SystemConfig.DataAccess
{
    public class DALCodeTable : DataAccessBase
    {
        public DALCodeTable()
        {
            if (this.DBHelper == null)
            {
                this.DBHelper = DBHelperFactory.CreateInstance(DbHelperName.Business);
            }
        }
        /// <summary>
        /// 通过Code_ID，Code_Type获取CodeTableID
        /// </summary>
        /// <returns></returns>
        public string GetCodeTableID(string Code_ID, string Code_Type)
        {
            string strsql = "SELECT CodeTableID FROM P_BT_CodeTable where  Code_ID ='" + Code_ID + "' and Code_Type = '" + Code_Type + "'";
            object obj = DBHelper.ExecuteScalar(strsql);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }


        /// <summary>
        /// 通过Code_Type获取所有的字典信息
        /// </summary>
        /// <param name="Code_Type">类型-不区分大小写</param>
        /// <returns></returns>
        public System.Data.DataSet GetCodeTableDataByType(string Code_Type)
        {
            string strsql = string.Format("SELECT * FROM P_BT_CodeTable where  UPPER(CODE_TYPE) = '{0}' order by order_num", Code_Type.ToUpper());
            System.Data.DataSet ds = this.DBHelper.ExecuteDataSet(strsql);
            return ds;
        }

        /// <summary>
        /// 获取系统核算币
        /// </summary>
        /// <returns></returns>
        public string GetAccountingCurrencyID()
        {
            string strsql = "SELECT CurrencyID FROM P_BT_Currency left outer join dbo.P_BT_CodeTable on P_BT_CodeTable.Code_Desc = P_BT_Currency.CurrencyID  where P_BT_CodeTable.CodeTableID = 'AccountingCurrency--0'";
            string obj = (string)DBHelper.ExecuteScalar(strsql);
            return obj;
        }
        #region add by 刘英男 20111102 码表描述
        /// <summary>
        /// 根据主键获取码表描述
        /// </summary>
        /// <param name="pkValue">主键</param>
        /// <returns>pkName</returns>
        public String GetpkNameByCodepkValueInfo(string pkValue)
        {
            string strSql = "select Code_Desc from P_BT_CodeTable where CodeTableID='" + pkValue + "'";
            object obj = DBHelper.ExecuteScalar(strSql);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        #endregion

        public System.Data.DataSet GetCodeTableDataByTypeWithOutDBHelperGive(string Code_Type)
        {
            string strsql = "SELECT * FROM P_BT_CodeTable where  Code_Type = '" + Code_Type + "' order by order_num";
            if (this.DBHelper != null)
            {
                System.Data.DataSet ds = this.DBHelper.ExecuteDataSet(strsql);
                return ds;
            }
            else
            {
                DreamBuilder.Core.DAO.IDBHelper dbhelper = DreamBuilder.Core.DAO.DBHelperFactory.CreateInstance("business");
                System.Data.DataSet ds = dbhelper.ExecuteDataSet(strsql);
                return ds;
            }
        }

        public System.Data.DataSet GetSingleWellCategory()
        {
            string strsql = "select * from p_bt_codetable where code_type like '%WellCategory%' order by order_num";
            if (this.DBHelper != null)
            {
                return this.DBHelper.ExecuteDataSet(strsql);
            }
            else
            {
                DreamBuilder.Core.DAO.IDBHelper dbhelper = DreamBuilder.Core.DAO.DBHelperFactory.CreateInstance("business");
                return dbhelper.ExecuteDataSet(strsql);
            }
        }
    }
}
